<?php

namespace app\api\controller;

use app\service\BaseService;
use think\Controller;
use think\Db;

class Info extends Common
{

	//挖矿运行
	public function wakuang()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$setting = $this->setting;

		$id =  input('id') ? input('id') : apifasong(['zt' => -1, 'msg' => '缺少参数']);
		$result = db('shop_orderform')->field(true)->where(array('id' => $id))->find();
		if(!$result)apifasong(['zt' => -1, 'msg' => '矿机不存在']);



		if ($result['zt'] == "0") {	//启动矿机
			if ($userinfo['level'] == 0) {
				$jhzt = db('user')->where(array('uname' => $userinfo['uname']))->update(array('is_effect' => 1));
			}
			// 矿机状态
			if (!$userinfo['kjzt']) {
				$he = db('user')->where(array('uname' => $userinfo['uname']))->update(array('kjzt' => 1));
			}
			$map['endtime'] = strtotime('+'.$result['yxzq'].' day'); // 订单
			$map['zt'] = 1; //
			$map['starttime'] = time(); //
			// 矿机状态
			$re = db('shop_orderform')->where(array('id' => $id))->update($map);
			if ($re) {
				if($result['kj_type'] == 7){
					$count  = db('invite_reward_coin')->where('from_uid',$userinfo['id'])->count();
					if(!$count){
						$invite_reward_coin['uid'] = $userinfo['tid'];
						$invite_reward_coin['uname'] = $userinfo['tname'];
						$invite_reward_coin['coin'] = $setting['tj_reward'];
						$invite_reward_coin['release_zq'] = $setting['tj_reward_time'];
						$invite_reward_coin['time'] = time();
						$invite_reward_coin['end_time'] = time() + ($setting['tj_reward_time'] * 24 * 60 * 60);
						$invite_reward_coin['from_uid'] = $userinfo['id'];
						$invite_reward_coin['from_uname'] = $userinfo['uname'];
						db('invite_reward_coin')->insert($invite_reward_coin);
					}
				}
				db('user')->where(array('uname' => $userinfo['uname']))->setInc('power',$result['kjsl']);
				apifasong(['zt' => 1, 'msg' => '启动矿机成功']);
			}else{
				apifasong(['zt' => -1, 'msg' => '矿机运行失败！']);
			}
		}else{
			apifasong(['zt' => -1, 'msg' => '矿机已在运行中']);
		}

	}

	// 我的矿机
	public function mykuangche()
	{

		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$map['uid'] = array('=',$userinfo['id']);
		$data = db('shop_orderform')->field('id, project, capacity,yxzq, addtime, type, zt')->where($map)->order("zt asc,id desc")->select();

		$count = 0;
		foreach ($data as &$v) {
			$v['addtime'] = date("Y-m-d H:i:s", $v['addtime']);
			// 查询昨日收益数量
			$money = db('userget')->where(array('kcid' => $v['id']))->whereTime('gettime','today')->value('money');
			// 是否有收益数量
			$money_count = db('userget')->where(array('kcid' => $v['id'],'earnings' => '1'))->whereTime('gettime','today')->value('money');
			// 已产出数量
			$v['output'] = db('userget')->where(array('kcid' => $v['id']))->sum('money');
			if ($money) {
				$v['money'] = $money;
			}else{
				$v['money'] = 0;
			}
			$count += $money_count;
		 }
		 $mykuangche['kuangchelist'] = $data;
		 $mykuangche['count'] = $count;
		 $mykuangche['power'] = $userinfo['power'];

		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $mykuangche]);
	}

	// 获取矿机收益(弃用)
	public function kj_earnings()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$setting = $this->setting;

		apifasong(['zt' => -1, 'msg' => '接口不可用']);

		if( !(date('H') >= $setting['income_start']   && date('H') <= $setting['income_end'])){
			apifasong(['zt' => -1, 'msg' => '收益时间'.$setting['income_start'].'-'.$setting['income_end']]);
		}

		$id =  (input('id')) ? input('id') : apifasong(['zt' => -1, 'msg' => '缺少参数']);

		$result = db('shop_orderform')->field(true)->where(array('id' => $id))->find();

		$shouyi['kcid'] = array('=',$id);
		$shouyi['earnings'] = 1;
		$money = db('userget')->where($shouyi)->whereTime('gettime','today')->sum('money');
		if (!$money) {
			apifasong(['zt' => 1, 'msg' => '暂无收益']);
		}

		// 启动事务
		Db::startTrans();
		try{

			$logocoin["uname"] = $userinfo['uname']; // 用户名
			$logocoin["uid"] = $userinfo['id']; // 金幣
			$logocoin["type"] = 6;
			$logocoin["mqian"] = $userinfo['idc'];//当前帐户金币余额
			$logocoin["money"] = $money;//变动数量
			$logocoin["mhou"] = $userinfo['idc'] + $money;//变动之后的
			$logocoin["note"] = '矿机收入';
			$logocoin["order_id"] = $result['id'];
			$logocoin["order_sn"] = $result['kjbh'];
			$logocoin["time"] = time();
			db('logocoin')->insert($logocoin);
			db('userget')->where($shouyi)->whereTime('gettime','today')->update(array('earnings' => 2, 'toobtain_time' => time()));
			db('user')->where(array('uname' => $userinfo['uname']))->setInc('idc' ,$money);
			// 提交事务
			Db::commit();
			$userdata = db('user')->field(true)->where(['uname' => $userinfo['uname']])->find();
			apifasong(['zt' => 1, 'msg' => '收取收益'.round($money,2),'userinfo' => apiuserbaseinfo($userdata)]);
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			apifasong(['zt' => -1, 'msg' => '获取收益失败']);
			//注意：我们做了回滚处理，所以id为1039的数据还在
		}

	}



	// 一键获取矿机收益
	public function onekey_kj_earnings()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$setting = $this->setting;

		if( !(date('H') >= $setting['income_start']   && date('H') <= $setting['income_end'])){
			apifasong(['zt' => -1, 'msg' => '收益时间'.$setting['income_start'].'-'.$setting['income_end']]);
		}

		// 查询昨日是否有收益凌晨00.00.1
		$shouyi['uname'] = array('=',$userinfo['uname']);
		$shouyi['earnings'] = 1;
		$money = db('userget')->where($shouyi)->whereTime('gettime','today')->sum('money');
		if (!$money) {
			apifasong(['zt' => 1, 'msg' => '暂无收益']);
		}

		// 启动事务
		Db::startTrans();
		try{
			$logocoin["uname"] = $userinfo['uname']; // 用户名
			$logocoin["uid"] = $userinfo['id']; // 金幣
			$logocoin["type"] = 6;
			$logocoin["mqian"] = $userinfo['idc'];//当前帐户金币余额
			$logocoin["money"] = $money;//变动数量
			$logocoin["mhou"] = $userinfo['idc'] + $money;//变动之后的
			$logocoin["note"] = '一键矿机全部收入';
			$logocoin["time"] = time();
			db('logocoin')->insert($logocoin);
			db('userget')->where($shouyi)->whereTime('gettime','today')->update(array('earnings' => 2, 'toobtain_time' => time()));
			db('user')->where(array('uname' => $userinfo['uname']))->setInc('idc' ,$money);

			// 直推（烧伤奖励）
			$this->shaoshang($userinfo['uname'],$userinfo['tname'],$setting['tj_reward_tname']);
            //上级邀请奖励币释放
            $res = BaseService::service()->t_release_coin($userinfo);
			// 提交事务
			Db::commit();
			$userdata = db('user')->field(true)->where(['uname' => $userinfo['uname']])->find();
			apifasong(['zt' => 1, 'msg' => '收取收益'.round($money,2),'userinfo' => apiuserbaseinfo($userdata)]);
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			apifasong(['zt' => -1, 'msg' => '获取收益失败']);
			//注意：我们做了回滚处理，所以id为1039的数据还在
		}

	}

	// 直推（烧伤奖励）调用
	public function shaoshang($uname, $tname = '',$tj_reward_tname)
	{
		// 无上级不执行
		if(!empty($tname)){

			$uname_shouyi['uname'] = $uname;
			$uname_userget = db('userget')->where($uname_shouyi)->whereTime('gettime','today')->select();

			$uname_shouyi['uname'] = $tname;
			$tname_userget = db('userget')->where($uname_shouyi)->whereTime('gettime','today')->column('kcid');

			// 上级无矿机收益不执行
			if($tname_userget){
				$tname_id = implode(',',$tname_userget);
				$tname_capacity_max = db('shop_orderform')->order('capacity desc')->where('id','in',$tname_id)->find();


				foreach($uname_userget as $v){
					$uname_capacity = db('shop_orderform')->where('id',$v['kcid'])->value('capacity');
					// 直推人小于邀请人矿机产能 ， 那么收取上级最大的产能矿机，昨日产出的百分之5
					if($tname_capacity_max['capacity'] < $uname_capacity){
						$tname_money = db('userget')->where('kcid',$tname_capacity_max['id'])->whereTime('gettime','today')->value('money');
						$tj_tname = $tname_money * ($tj_reward_tname / 100);
					}else{
						$tj_tname = $v['money'] * ($tj_reward_tname / 100);
					}


					$user_tname = db('user')->where('uname',$tname)->find();
					$logocoin["uname"] = $user_tname['uname']; // 用户名
					$logocoin["uid"] = $user_tname['id']; // 金幣
					$logocoin["type"] = 12;
					$logocoin["mqian"] = $user_tname['idc'];//当前帐户金币余额
					$logocoin["money"] = $tj_tname;//变动数量
					$logocoin["mhou"] = $user_tname['idc'] + $tj_tname;//变动之后的
					$logocoin["note"] = '直推奖励';
					$logocoin["time"] = time();
					db('logocoin')->insert($logocoin);
					db('user')->where('uname',$user_tname['uname'])->setInc('idc',$tj_tname);
				}
			}
		}

	}



}
